<?php
namespace Admin\Controller;

class ClassmanagerController extends BaseController {


    public function index() {

        $map['department_id'] = $this->admin['department_id'];

        $Teacher = D ('Teacher');
        $teacherList = $Teacher->getTeacherByWhere($map);

        $ClassManager = D ('ClassManager');
        $Class = D ('Class');

        $data = array();
        foreach ($teacherList as $key=>$val) {
            $cids = $ClassManager->getClassIdsByTid($val['id']);
            if (!is_null($cids)) {
                $c = array();
                foreach ($cids as $v) {
                    $c['id'] = $v;
                    $c['name'] = $Class->getFullClassName($v);
                    $data[$key]['class'][] = $c;
                }
                $data[$key]['teacher'] = $val;
                $data[$key]['id'] = $ClassManager->getClassManagerIdByTid($val['id']);
            }
        }

        $this->assign('data',$data);
        $this->display();
    }


    public function add() {
        if (!IS_POST) {

            $ClassManager = D ('ClassManager');
            $Class = D ('Class');

            // 获取空闲的教师
            $map['department_id'] = $this->admin['department_id'];
            $tids = $ClassManager->getAllTeacherIds();

            !empty($tids) && $map['teacher_id'] = array('not in',$tids);

            $Teacher = D ('Teacher');
            $teacherList = $Teacher->getTeacherByWhere($map);

            // 获取所有未被管理的班级
            $cids = $ClassManager->getAllClassIds();
            unset ($map['teacher_id']);
            !empty($cids) && $map['id'] = array('not in',$cids);

            $classList = $Class->getClassByWhere($map,'',true);


            $this->assign('teacherList',$teacherList);
            $this->assign('classList',$classList);
            $this->display();
        } else {

            $data['department_id'] = $this->admin['department_id'];
            $data['teacher_id'] = I ('request.teacher_id','');
            $cids = I ('request.class_id','');
            if (empty($cids))
                $this->out('RESULT_FAIL','未选择班级');

            $data['class_id'] = implode(',',$cids);
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $ClassManager = D ('ClassManager');
            $flag = $ClassManager->_add($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }
    }



    public function edit() {
        if (!IS_POST) {
            $id = I ('request.id','');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $ClassManager = D ('ClassManager');
            $c = $ClassManager->_get($id);
            if (empty($c))
                $this->error('内容不存在');

            $c['teacher_name'] = D ('Teacher')->getTeacherName($c['teacher_id']);
            $cidArr = explode(',',$c['class_id']);

            // 获取所有未被管理和该老师已管理班级ID
            $Class = D ('Class');
            $where['teacher_id'] = array('neq',$c['teacher_id']);
            $cids = $ClassManager->getAllClassIds($where);
            !empty($cids) && $map['id'] = array('not in',$cids);
            $map['department_id'] = $this->admin['department_id'];

            $classList = $Class->getClassByWhere($map);
            foreach ($classList as $k=>$v) {
                if (!in_array($v['id'],$cidArr)) continue;
                $classList[$k]['selected'] = 'selected';
            }

            $this->assign('classManager',$c);
            $this->assign('classList',$classList);
            $this->display();
        } else {

            $data['id'] = I ('request.id');
            $cids = I ('request.class_id','');
            if (empty($cids))
                $this->out('RESULT_FAIL','未选择班级');

            $data['class_id'] = implode(',',$cids);

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $ClassManager = D ('ClassManager');
            $flag = $ClassManager->_edit($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }



    public function del() {
        if (!IS_POST) $this->error('非法访问');
        $id = I ('request.id');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $ClassManager = D ('ClassManager');
        $flag = $ClassManager->_del($id);
        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

}